﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.TypeManage
{
    public partial class ProjectTypeList : SecurityPage
    {
        public override string PrivCode()
        {
            string code = "";
            switch((EnumData.ProjectType)int.Parse(this.GetTypeId()))
            {
                case EnumData.ProjectType.公益活动:
                    code = "35535c564ae02795";
                    break;
                case EnumData.ProjectType.勤工助学:
                    code = "118cd2973b3b77b5";
                    break;
                case EnumData.ProjectType.社会实践:
                    code = "230ba6b8e2a1d28b";
                    break;
                case EnumData.ProjectType.新闻分类:
                    code = "c7d94a541359e86f";
                    break;
                case EnumData.ProjectType.职位性质:
                    code = "2ae77d70acc111a";
                    break;
                case EnumData.ProjectType.志愿活动:
                    code = "1c3a224a7a58d3b";
                    break;
            }
            return code;
        }

        int i = 0;
        public string GetXN()
        {
            if (i == 0)
                i = (this.AspNetPager1.PageSize) * (this.AspNetPager1.CurrentPageIndex - 1);

            i += 1;
            return i.ToString();
        }

        public string GetTypeId()
        {
            return Request.QueryString["TypeId"].ToString();
        }

        private void RepBanding()
        {
            int TypeId=int.Parse(this.GetTypeId());

            int RowCount = TypeInfo.Where("ProjectTypeid=@0 and IsDel=@1", TypeId,false).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<TypeInfo> list = TypeInfo.Where("ProjectTypeid=@0 and IsDel=@1", TypeId,false).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
        }

        protected void Rep_DataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int NewId = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                TypeInfo md = TypeInfo.Where("id=@0", NewId).First();
                md.IsDel = true;
                TypeInfo.Update(md, "id=@0", NewId);
                //TypeInfo.Delete("id=@0", NewId);
                RepBanding();
            }
            else
            {
                TypeInfo md = TypeInfo.Where("id=@0", NewId).First();
                UtilityUI.Load(this.UpdatePanel1, "LoadData('" + md.TypeName + "','" + md.id + "')");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "0")
            {
                //添加
                TypeInfo md = new TypeInfo();
                md.IsDel = false;
                md.AddTime = DateTime.Now;
                md.TypeName = this.txt_TypeName.Text.Trim();
                md.ProjectTypeid = int.Parse(this.GetTypeId());
                TypeInfo.Insert(md);
                this.RepBanding();
            }
            else
            { 
                //修改
                int Typeid = int.Parse(this.HidTypeId.Value);
                TypeInfo md = TypeInfo.Where("id=@0", Typeid).First();
                md.TypeName = this.txt_TypeName.Text.Trim();
                TypeInfo.Update(md, "id=@0", Typeid);
                this.RepBanding();
            }
        }

        protected void Btn_AddType_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProjectTypeEdit.aspx?TypeId="+this.GetTypeId());
        }
    }
}